package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.think.common.utils.Common;
import com.think.teaching.model.BaseModel;

import java.io.Serializable;

public class BaseServiceImpl<TM extends BaseMapper<TE>,
        TE extends BaseModel<TE >> extends ServiceImpl<TM,TE> {

    @Override
    public TE getById(Serializable id) {
        QueryWrapper<TE> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("id",id).ne("delflag",1);
        return this.getOne(queryWrapper);
    }
    
    
    public Boolean DeleteObjectById(Integer id) {
    	return this.removeById(id);
    }
    
    public boolean ChangeObjectState(Integer id,Integer state) {
    	UpdateWrapper<TE> updateWrapper = new UpdateWrapper<>();
    	updateWrapper.eq("id", id).set("state", Math.max(0, state));
    	return this.update(updateWrapper);
    }
    
    public TE AddObjectEntity(TE data) {
    	data.setCreatedBy(Common.GetCurrentAuthor().getUserId());
    	return this.save(data)?data:null;
    }

}
